<template>
  <el-table-column
      prop="dept_name"
      label="门店名称"
      min-width="200"
  >
    <template #default="{ row }">
      <!-- 如果是公司，显示不同的样式 -->
      <template v-if="row.pid === -1">
        <span
            :class="{
            'selected': selectedRow?.id === row.id && selectable,
            'hoverable': hoverable
          }"
            @click="handleRowClick(row)"
        >
          {{ row.shop_name }}
        </span>
      </template>
      <template v-else>
        <span
            :class="{
            'selected': selectedRow?.id === row.id && selectable,
            'hoverable': hoverable
          }"
            @click="handleRowClick(row)"
        >
          {{ row.shop_name }}
        </span>
      </template>
    </template>
  </el-table-column>
</template>

<script setup lang="ts">
import type { OrganizationNameColumnProps } from '@/types/components'
import { ref } from 'vue'

// 使用defineProps来获得类型检查
const props = withDefaults(defineProps<{
  selectable?: boolean  // 是否可选中
  hoverable?: boolean   // 是否显示悬停效果
}>(), {
  selectable: false,
  hoverable: false
})

// 定义emit
const emit = defineEmits<{
  (e: 'select', row: any): void
}>()

// 当前选中的行
const selectedRow = ref<any>(null)

// 处理行点击
const handleRowClick = (row: any) => {
  if (!props.selectable) return
  selectedRow.value = row
  emit('select', row)
}
</script>

<style lang="scss" scoped>
span {
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  display: inline-block;
  position: relative;
  border-left: 2px solid transparent;  // 添加透明边框，防止选中时的抖动

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: transparent;
    border-radius: 4px;
    transition: background-color 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: -1;
  }
}

.hoverable {
  &:hover {
    transform: translateX(2px);

    &::before {
      background-color: #f0f2f5;
    }
  }
}

.selected {
  color: #1890ff;
  border-left: 2px solid #1890ff;
  font-weight: 500;

  &::before {
    background-color: #e6f7ff;
    box-shadow: 0 2px 6px rgba(24, 144, 255, 0.1);
  }
}
</style>
